package cn.auth.service.impl;

import cn.auth.config.web.DataGridResultInfo;
import cn.auth.dao.UserDaoMapper;
import cn.auth.pojo.User;
import cn.auth.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 毛苏宇
 * User: me
 * Date: 2020/6/24
 * Time: 15:27
 * Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDaoMapper userDaoMapper;

    @Override
    public User find(User user) {
        return userDaoMapper.selectOne(user);
    }

    @Override
    public boolean existsByUserName(String username) {
        long count = userDaoMapper.countByName(username);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public long add(User user) {
        user.setUpdateTime(System.currentTimeMillis());
        //没有该用户则插入
        return userDaoMapper.insert(user);
    }

    @Override
    public boolean modify(User user) {
        user.setUpdateTime(System.currentTimeMillis());
        return userDaoMapper.update(user) > 0 ? true : false;
    }

    @Override
    public boolean remove(long userId) {
        return userDaoMapper.delete(userId) > 0 ? true : false;
    }

    @Override
    public boolean existsById(long userId) {
        return userDaoMapper.countById(userId) > 0 ? true : false;
    }

    @Override
    public DataGridResultInfo<User> findUsers(int page, int rows) {
        Page<User> userPage = PageHelper.startPage(page, rows);
        userDaoMapper.list();
        DataGridResultInfo<User> dataGridResultInfo = new DataGridResultInfo<>();
        dataGridResultInfo.setEndRow(userPage.getEndRow());
        dataGridResultInfo.setPageNum(userPage.getPageNum());
        dataGridResultInfo.setPages(userPage.getPages());
        dataGridResultInfo.setPageSize(userPage.getPageSize());
        dataGridResultInfo.setRows(userPage.getResult());
        dataGridResultInfo.setStartRow(userPage.getStartRow());
        dataGridResultInfo.setTotal(userPage.getTotal());
        return dataGridResultInfo;
    }


}
